package com.worley.argutils;

import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

@Deprecated
public final class RedisThreadPool {

    private static final Logger logger = LoggerFactory
            .getLogger(RedisThreadPool.class);

    // Redis服务器IP
    private static String ADDR = "172.16.9.138";
    // Redis的端口号
    private static int PORT = 6381;
    // 访问密码
    private static String PASSWORD;
    // 最大连接数,默认值8
    private static int MAX_TOTAL = 8;
    // 连接最大等待时长
    private static long MAX_WAIT_MILLIS = 3000;
    // 控制一个pool最多有多少个状态为idle(空闲的)的jedis实例，默认值也是8。
    private static int MAX_IDLE = 8;
    // 连接超时时长
    private static int TIMEOUT = 1000;
    // 在borrow一个jedis实例时，是否提前进行validate操作；如果为true，则得到的jedis实例均是可用的；
    private static boolean TEST_ON_BORROW = true;
    // 在空闲时检查有效性
    private static boolean TEST_WHIEL_IDEL = true;

    private static JedisPool jedisPool = null;

    private static JedisProxy jedisProxy = null;

    private static boolean isCluster;
    
    /**
     * 初始化Redis连接池
     */
    static {

        initJedisCluster();
        isCluster = true;
    }

    private static void initJedisCluster() {
        GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
        poolConfig.setMaxTotal(MAX_TOTAL);
        JedisCluster jedisCluster = new JedisCluster(new HostAndPort(ADDR, PORT), 3000,
                3000, 2, PASSWORD, poolConfig);
        jedisProxy = new JedisProxy(jedisCluster);
    }

    private static void initJedisPool() {
        try {
            JedisPoolConfig config = new JedisPoolConfig();
            config.setMaxIdle(MAX_IDLE);
            config.setTestOnBorrow(TEST_ON_BORROW);
            config.setTestWhileIdle(TEST_WHIEL_IDEL);
            config.setMaxTotal(MAX_TOTAL);
            config.setMaxWaitMillis(MAX_WAIT_MILLIS);
            jedisPool = new JedisPool(config, ADDR, PORT, TIMEOUT, PASSWORD);
        } catch (Exception e) {
            logger.error("初始化Redis连接池失败:", e);
        }
    }

    /**
     * 获取Jedis实例
     * 
     * @return
     */
    public static synchronized Jedis getJedis() {
        try {
            if (isCluster) {
                return jedisProxy;
            } else if (jedisPool != null) {
                return jedisPool.getResource();
            } else {
                return null;
            }
        } catch (Exception e) {
            logger.error("从Redis连接池中获取Reis连接失败", e);
            return null;
        }
    }

    /**
     * 获取redis库连接
     * 
     * @param sCode
     * @return
     */
    public static Jedis getJedis(int dbNum) {
        if (isCluster) {
            logger.warn("Cluster模式不支持选择数据库：{}", dbNum);
            return null;
        } else {
            // 从Redis连接池中获取一个连接
            Jedis jedis = RedisThreadPool.getJedis();
            jedis.select(dbNum);
            return jedis;
        }
    }
}
